<?php

namespace Admin\Controller;

use \Org\Util\Rest;
use Org\BMap; 

class TestController extends AdminController {

    /**
     * 下拉
     */
    public function test(){
        $rest = new Rest();
        
        $res = $rest->getList('GroupPurchaseOrder');
        $list = $res->body->results;
        
        $list = $this->pullParse('gp_order','GroupPurchaseOrder',
            array(),
            function($src){
                //前调函数
                $src['createDate'] = strtotime($src['createDate']);
                $src['lastupdate'] = strtotime($src['lastupdate']);
                
                //修改 地址信息
                $address = (array)($src['address']);
                $src['toStreet'] = $address['address'];
                $src['toPhone'] = $address['phone'];
                $src['toName'] = $address['name'];
                $src['toCity'] = $address['city'];
                $src['toArea'] = $address['area'];
                $src['toProvince'] = $address['province'];
                
                //修改 经纬度信息
                $location = $src['location'];
                $src['toLon'] = substr($location, 0,strpos($location,','));
                $src['toLat'] = substr($location, strpos($location,',')+1);
                
                //划分区域
                $GeoUtils=new BMap\GeoUtils();
                $areaId=$GeoUtils->isPointInArea($src['toLon'],$src['toLat']);
                $src['areaId'] = $areaId;
                return $src;
            },
            function($src){
                //后调函数
                $goods = $src['goods'];
                $list = array();
                foreach($goods as $g){
                    $g = (array)$g;
                    $list[] = array('orderid' => $g['orderid'],
                        'pid'=>$g['sid'],'cartnum'=>$g['size'],'createdate'=>0,'lastupdate'=>0,'syncStatus'=>1);
                }
                
                $m = M('order_goods');
                $flag = $m->addAll($list);
                $src['goodsFlag'] = $flag;
                
                return $src;
            },
            $list);
            dump($list);
    }

    /**
     * 把云端数据下拉到本地数据库中
     * @param type $table  本地的表名
     * @param type $map  本地的字段对应云端的字段
     * @param type $before 前调函数 （插入数据库之前操作的操作）
     * @param type $after  后调函数 （插入数据库之后操作的操作）
     * @param type $array  数据源
     * @author James 2015-06-29
     */
    public function pullParse($table,$class,$map,$before,$after,$array){
        
        $map['_table'] = $table;
        $map['_class'] = $class;
        $map['_before'] = $before;
        $map['_after'] = $after;
        return array_map(function($src,$map){
            $table = $map['_table'];
            $class = $map['_class'];
            $before = $map['_before'];
            $after = $map['_after'];
            unset($map['_table']);
            unset($map['_class']);
            unset($map['_after']);
            unset($map['_before']);
            $src = (array)$src;
            $row = array();
            
            if(count($map)>0){
                foreach($map as $k=>$v){
                    $row[$k] = $src[$v];
                }
            }else{
                $row = $src;
            }
            
            $row = call_user_func($before,$row);
            
            $m = M($table);
            $rest = new Rest();
            switch($src['syncStatus']){
                case 1:
                    //删除
                    $m->add($row);
                    $rest->put($class,$src['objectId'],json_encode(array('syncStatus'=>0)));
                    break;
                case 2:
                    //修改
                    $m->where(array("id"=>$row['id']))->save($row);
                    $rest->put($class,$src['objectId'],json_encode(array('syncStatus'=>0)));
                    break;
                case 3:
                    //删除
                    $m->where(array("id"=>$row['id']))->delete();
                    break;
            }
            
//            $row['autoid'] = $id;
            $row = call_user_func($after,$row);
            return $row;
        },$array,array_fill(0,count($array),$map));

    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    public function pushParse($class,$map,$before,$after,$array){

        $map['_class'] = $class;
        $map['_before'] = $before;
        $map['_after'] = $after;
        return array_map(function($src,$map){
            $class = $map['_class'];
            $before = $map['_before'];
            $after = $map['_after'];
            unset($map['_class']);
            unset($map['_after']);
            unset($map['_before']);

            $src = call_user_func($before,$src);
            $row = array();
            foreach($map as $k=>$v){
                $row[$k] = $src[$v];
            }

            //上推代码
            $rest = new Rest();
            switch($src['syncStatus']){
                case 1:
                    $row['syncStatus'] = 0;
                    $rst = $rest->add($class,json_encode($row));
                    dump($rst);
                    break;
                case 2:
                    $row['syncStatus'] = 0;
                    $rst = $rest->put($class,$row['objectId'],json_encode($row));
                    dump($rst);
                    break;
                case 3:
                    $rst = $rest->delete($class,$row['objectId']);
                    break;
            }
            $row['flag'] = $rst;
            $row = call_user_func($after,$row);
            return $row;
        },$array,array_fill(0,count($array),$map));

    }

    public function test1(){

        //取数据
        //存放到数组
        //过滤一遍数组
        //调用回调函数处理数据
        //rest请求
        //返回处理完成的数据

        $m = M('test');

        $list = $m->where(array('syncStatus'=>array('egt', 1)))->select();

        $list = $this->pushParse('Test',
            array('objectId'=>'id','name'=>'name','age'=>'age','status'=>'status','syncStatus'=>'syncStatus'),
            function($src){
                $src['age'] = intval($src['age']);
                $src['status'] = intval($src['status']);
                return $src;
            },
            function($src){
                $m = M('test');
                $m->where('id='.$src['objectId'])->data(array('syncStatus'=>0))->save();
                return $src;
            },
            $list);
    }

}
